<?php
/**个人中心模型
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/6
 * Time: 22:17
 */

namespace Common\Model;



class MyCenterModel  extends BaseModel
{
    //主键
    protected  $pk  =  'oid';
    //要操作那张表
    protected  $tableName  =  'order';
    //自动验证
    protected $_validate   = [

    ];

    //订单管理方法
    public function addOrder(){

        //1、先添加订单表数据
        //1.1重组data数据  总价格，地址id，时间，用户id，订单状态
        $data  = [
            'total'=>$_POST['order_total_price'],
            'adid'=>$_POST['order_aid'],
            'time'=>time(),
            'uid'=>session('home.uid'),
            'status'=>'待发货'

        ];
        //1.2判断是否需要开发票,通过一个标识来判断，
        if(I('post.invoice')==1){
                if( I('post.p_or_c')=='个人'){
                    $data['note']='个人发票';
                }else{
                    $data['note'] = '单位名称：'.I('post.danweiname');
                }
        }else{
            $data['note'] = '无';
        }
        //1.3定义一个订单号并压入$data数组,要求不重复
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $data['number'] = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        //1.4执行add添加动作
        $oid =  $this->add($data);


        //2、添加订单列表数据
        //2.1、获得post的数据
        //$gtid  = ($_SESSION['order']['gtid']);
        //$temp = [];
        //$number = [];
        //foreach($gtid as $k=>$v){
        //    $temp[$k] = (new GoodsListsModel())->where("grid='{$v}'")->getField("prid");
        //    $number[$k] = (new GoodsListsModel())->where("grid='{$v}'")->getField("pnumbers");
        //
        //}
        //$oldOredrData  = I('post.orderData');
        $oldOredrData  = session('order.order');
        ////因为$k值相等的所以能压到同一个数组中
        //foreach ($oldOredrData as  $k=>$v){
        //        $oldOredrData[$k]['prid'] =$temp[$k];
        //        $oldOredrData[$k]['glnumber']=$number[$k];
        //}
        //2.2、将post的数据循环重组 商品id，数量，价格，订单id，货品id，货品号
        foreach($oldOredrData as $k=>$v){
                $data = [
                    'gid'=>$v['gid'],
                    'quantity'=>$v['num'],
                    'price'=>$v['price'],
                    'oid'=>$oid,
                ];

            //2.3、执行add添加动作
            (new OrderListModel())->add($data);
        }
        //3、更新购物车中的数据（将已经付款的订单列去除）
        $sids  =  session('selected_sids');
        foreach($sids as $k=>$v){
            //dd($_SESSION['cart']['goods'][$v]);die;
                unset($_SESSION['cart']['goods'][$v]);
        }
        return ['status'=>'success','message'=>'订单提交成功'];
    }

}